<?php

$loader->requireOnce('includes/GeneralReport/PortletGReportLineHollow.class.php');
$loader->requireOnce('includes/GeneralReport/PortletGReport.class.php');

class C_GeneralReport extends Controller {

public function C_GeneralReport($template_mod = "general") {
	parent::Controller();
	if (Celini::getCurrentController() == 'GeneralReport') {
			$menu =& Menu::getInstance();
			$menu->currentSection = 'patient';
		}
	$this->template_mod = $template_mod;
	$this->view->path 	= 'general_report';
}

public function actionViewAllReports(){
	$userId		=	$this->_me->get_person_id();
	$groups		=	Celini::newORDO('GeneralReportGroup')->getAllGroups($userId);
	$this->view->assign('groups', $groups);

	return $this->view->render('view_all_reports.html');
}

public function actionRenderReports($groupId = 'all'){
	if(isset($_GET['groupId'])){
		$groupId	=	$_GET['groupId'];
	}
	$userId		=	$this->_me->get_person_id();
	// Get report groups
	if($groupId==='all'){
		$groupId	=	null;
		$groups		=	Celini::newORDO('GeneralReportGroup')->getAllGroups($userId);
	}
	else{
		$group	=	Celini::newORDO('GeneralReportGroup', $groupId);
		$groups[$groupId]	=	$group->getName();
	}
	// Get reports
	$reports	=	Celini::newORDO('GeneralReport')->getReports($userId, $groupId);
	// After get reports and report groups data, build report bundles by grouping reports by report group
	$reportBundles	=	array();
	foreach ($groups as $grpId => $name){
		$reportBundles[$grpId]	=	array();
	}
		// Iterate all reports to grouping thems
	foreach($reports as $report){
			array_push($reportBundles[$report['group_id']], $report);
	}

	$this->view->assign('groups', $groups);
	$this->view->assign('reportBundles', $reportBundles);

	return $this->view->render('all_reports.html');
}

public function actionDeleteGroup($groupId = 0, $deleteReports = false){
	if(isset($_GET['groupId'])){
		 $groupId	=	(int)$_GET['groupId'];
	}

	if(isset($_GET['deleteReports'])){
		$deleteReports	=	(bool)$_GET['deleteReports'];
	}
	$obiReport	=	Celini::newORDO('GeneralReport');
	$objGroup	=	Celini::newORDO('GeneralReportGroup');
	if($deleteReports){
		$obiReport->deleteReports($groupId);
	}
	else{
		$obiReport->moveReportsToDefaultGroup($groupId);
	}
	$objGroup->deleteGroup($groupId);

	$response['deleted']	=	1;
	return json_encode($response);
}

public function actionLoadEditGroupPage($groupId = -1){
	if(isset($_GET['groupId'])){
		$groupId	=	(int)$_GET['groupId'];
	}
	$objGroup	=	Celini::newORDO('GeneralReportGroup');
	if($groupId > -1){
		$objGroup->setup($groupId);
	}

	$this->view->assign_by_ref('group', $objGroup);
	$this->view->assign('groupId', $groupId);

	return $this->view->render('edit_group.html');
}

public function actionSaveGroupInfo(){
	$objGroup	=	Celini::newORDO('GeneralReportGroup');
	$groupId	=	$_POST['groupId'];
	$groupName	=	Utils::sanitizeText($_POST['groupName']);
	$groupDesc	=	Utils::sanitizeText($_POST['groupDesc']);
	if($groupId > -1){
		$objGroup->setup($groupId);
	}
	$objGroup->setData($groupName, $groupDesc, $this->_me->get_person_id());
	$objGroup->persist();

	$response['code']  =   1;
	$response['message']  =   1;
	return json_encode($response);
}

public function actionLoadEditReportPage($reportId = 0){
	if(isset($_GET['reportId'])){
		$reportId	=	(int)$_GET['reportId'];
	}
	$objReport	=	Celini::newORDO('GeneralReport');
	if($reportId > 0){
		$objReport->setup($reportId);
	}
	$groups		=	Celini::newORDO('GeneralReportGroup')->getAllGroups($this->_me->get_person_id());
	$dataTypes	=	Celini::newORDO('GeneralReport')->getDataTypeList();
	$this->view->assign_by_ref('report', $objReport);
	$this->view->assign('reportId', $reportId);
	$this->view->assign('groups', $groups);
	$this->view->assign('dataTypes', $dataTypes);

	return $this->view->render('edit_report.html');
}

public function actionSaveReportInfo(){
	$objReport	=	Celini::newORDO('GeneralReport');
	$reportId	=	$_POST['reportId'];
	$reportName	=	Utils::sanitizeText($_POST['reportName']);
	$reportDesc	=	Utils::sanitizeText($_POST['reportDesc'], false);
	$ownerGroup	=	$_POST['ownerGroup'];
	$metricName	=	Utils::sanitizeText($_POST['metricName']);
	$dataName	=	Utils::sanitizeText($_POST['dataName']);
	$dataType	=	$_POST['dataType'];
	$dataUnit	=	Utils::sanitizeText($_POST['dataUnit']);
	if($reportId > 0){
		$objReport->setup($reportId);
	}
	$objReport->setData($reportName, $reportDesc, $ownerGroup, $metricName,
			$dataName, $dataType, $dataUnit, $this->_me->get_person_id());
	$objReport->persist();

	return json_encode(array("saved" => 1, "reportId" => $objReport->get_id()));
}

public function actionLoadInputDataPage($reportId = 0){
	if(isset($_GET['reportId'])){
		$reportId	=	(int)$_GET['reportId'];
	}

	$objReport	=	Celini::newORDO('GeneralReport', $reportId);
	$data		=	array();
	$data		=	Celini::newORDO('GeneralReportData')->getReportData($reportId);
	$rowNumbers	=	GeneralReport::DEFAULT_ROW_NUM;
	if(count($data) > 0){
		$rowNumbers	=	count($data);
	}
	$this->view->assign('rowNumbers', $rowNumbers);
	$this->assign('data', $data);
	$this->view->assign_by_ref('report', $objReport);

	return $this->view->render('input_data.html');
}

public function actionViewReport($reportId = 0){
	if(isset($_GET['reportId'])){
		$reportId	=	$_GET['reportId'];
	}
	$report		=	Celini::newORDO('GeneralReport', $reportId);
	$unit  =   '';
	if ($report->getDataType() == 'currency') {
        $unit   =   "(".L10nHelper::_('ĐVT').": ". $report->getDataUnit() . ")";
	}
	$data		=	array();
	$data		=	Celini::newORDO('GeneralReportData')->getReportData($reportId);
	if(count($data) > 0){
		$this->assign('data', $data);
	}
	$lineHollowUrl  =   urlencode("renderReport?reportId=$reportId&type=line_hollow&set_ajax_view");
	$this->assign('report', $report);
	$this->assign('reportId', $reportId);
	$this->assign('lineHollowUrl', $lineHollowUrl);
	$this->assign('unit', $unit);

	return $this->view->render('view_report.html');
}

public function actionRenderReport($reportId = 0, $type = 'line_hollow'){
	if(isset($_GET['reportId'])){
		$reportId	=	$_GET['reportId'];
	}
	if(isset($_GET['type'])){
		$type	=	$_GET['type'];
	}
	if($type=='line_hollow'){
		$objRender	=	new PortletGReportLineHollow($reportId);
	}
	else{
		$objRender	=	new PortletGReport();// FIXME: correct this
	}
	return $objRender->renderChart();
}

public function actionSaveReportData(){
	$i	=	0;
	$reportId	=	$_POST['reportId'];
	$objData	=	Celini::newORDO('GeneralReportData');
	// Delete current data of the report before inserting new data
	$objData->deleteData($reportId);
	while(isset($_POST['metric'][$i]) && !empty($_POST['metric'][$i])){
		$order	=	(int)Utils::sanitizeText($_POST['orders'][$i]);
		$metric	=	Utils::sanitizeText($_POST['metric'][$i]);
		$data	=	Utils::sanitizeText($_POST['data'][$i]);
		$objData->setData($reportId, $metric, $data, $order);
		$objData->persist();
		$objData->clearData();
		$i++;
	}

	$html	=	self::actionViewReport($reportId);
	$redirectUrl   =   Celini::link('viewReport', 'GeneralReport') . "reportId=$reportId";
    $response['url']    =   $redirectUrl;
    $response['code']   =   1;
	return json_encode($response);
}

public function actionDeleteReport($reportId){
	if(isset($_GET['reportId'])){
		$reportId	=	$_GET['reportId'];
	}
	Celini::newORDO('GeneralReport')->deleteReport($reportId);
}
}
?>